<?php 
	require_once '../function.php';

	function login(){
		if (empty($_POST['username'])) {
			$GLOBALS['message'] = '请输入用户名';
			return;
		}
		if (empty($_POST['password'])) {
			$GLOBALS['message'] = '请输入密码';
			return;
		}

		$username = $_POST['username'];
		$password = $_POST['password'];

		$query = xiu_excute_db("select * from users where username = '{$username}' limit 1;");

		$user = mysqli_fetch_assoc($query);

		if (!$user) {
			$GLOBALS['message'] = '用户名或密码错误';
			return;
		}
		if ($user['password'] !== $password) {
			$GLOBALS['message'] = '用户名或密码错误';
			return;
		}
		if ($user['status'] === '0') {
			$GLOBALS['message'] = '用户被禁用，请联系管理员';
			return;
		}

		$_SESSION['index_user'] = $user;

		header('Location: /index/index.php');
	}


	if ($_SERVER['REQUEST_METHOD'] === 'POST') {
		login();
	}
?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>登陆</title>
	<link rel="stylesheet" type="text/css" href="/static/vendors/bootstrap4.6/css/bootstrap.css">
	<link rel="stylesheet" type="text/css" href="/static/vendors/animate/animate.css">
	<link rel="stylesheet" type="text/css" href="/index/index.css">
</head>
<body>
	<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
		<div class="index-login">
			<div class="login-title">
				<h2>欢迎登陆Blog</h2>
			</div>
			<?php if (isset($message)): ?>
				<div class="animated shake">
					<div class="alert alert-danger"><?php echo $message; ?></div>
				</div>
			<?php endif ?>
			<div class="form-group">
				<label for="username">用户名</label>
				<input type="text" class="form-control" id="username" name="username" placeholder="用户名" autocomplete="off" value="<?php echo isset($_POST['username'])? $_POST['username'] : ''; ?>">
			</div>
			<div class="form-group">
				<label for="password">密码</label>
				<input type="password" class="form-control" id="password" name="password" placeholder="密码" autocomplete="off">
			</div>
			<button type="submit" class="btn btn-primary form-control">登陆</button>
		</div>
	</form>
</body>
</html>